<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="jquery.js" type="text/javascript"></script>
<SCRIPT src="javascripts/pidcrypt.js"></SCRIPT>
<SCRIPT src="javascripts/pidcrypt_util.js"></SCRIPT>
<SCRIPT src="javascripts/asn1.js"></SCRIPT>
<!--needed for parsing decrypted rsa certificate-->
<SCRIPT src="javascripts/jsbn.js"></SCRIPT>
<!--needed for rsa math operations-->
<SCRIPT src="javascripts/rng.js"></SCRIPT>
<!--needed for rsa key generation-->
<SCRIPT src="javascripts/prng4.js"></SCRIPT>
<!--needed for rsa key generation-->
<SCRIPT src="javascripts/rsa.js"></SCRIPT>
<!--needed for rsa en-/decryption-->
<script type="text/javascript">
function handleFileSelect(evt) {
	var files = evt.target.files; // FileList object

	// Loop through the FileList and render image files as thumbnails.
	for ( var i = 0, f; f = files[i]; i++) {
		var reader = new FileReader();
		// Closure to capture the file information.
		reader.onload = (function(theFile) {
			return function(e) {
				// Render thumbnail.
				document.getElementById('privateKey').value = e.target.result;
			};
		})(f);

		// Read in the image file as a data URL.
		reader.readAsText(f, 'UTF8');
	}
}
function decrypt() {
	var crypted = document
			.getElementById("pwhash").value;
	var private_key = document
			.getElementById("privateKey").value;

	params = certParser(private_key);
	if (params.b64) {
		key = pidCryptUtil
				.decodeBase64(params.b64);
		var rsa = new pidCrypt.RSA();
		//RSA decryption
		//ASN1 parsing
		asn = pidCrypt.ASN1
				.decode(pidCryptUtil
						.toByteArray(key));
		tree = asn.toHexTree();
		//alert(showData(tree));
		//setting the private key for encryption
		rsa.setPrivateKeyFromASN(tree);
		t = new Date(); // timer
		crypted = pidCryptUtil
				.decodeBase64(pidCryptUtil
						.stripLineFeeds(crypted));
		var decrypted = rsa
				.decrypt(pidCryptUtil
						.convertToHex(crypted));
		document
				.getElementById('decrypted').value = decrypted;
	} else {
		alert('invalid key or I\'m too stupid to parse');
	}
}

	$(document).ready(function() {
		$('#decryptBtn')
		.click(decrypt
				);
		 document.getElementById('files').addEventListener('change', handleFileSelect, false);
		$('#decryptBtn').click(decrypt);

	});
</script>

</head>
<body>
<table>
			<tr>
				<td>PW-Hash</td>
				<td><textarea id="pwhash"></textarea></td>
			</tr>
			<tr>
				<td>Private Key</td>
				<td><textarea id="privateKey"></textarea>
				</td>
				<td><input type="file" id="files" name="files[]" />
				</td>
			<tr>
				<td>Decrypted</td>
				<td><textarea id="decrypted"></textarea></td>
			</tr>
			<tr>
				<td />
				<td><input type="button" value="Decrypt" id="decryptBtn" />
			</tr>
		</table>
</body>
</html>